<?php

namespace app\admin\controller\my;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;

/**
 * @ControllerAnnotation(title="my_delegation")
 */
class Delegation extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\MyDelegation();
        
    }

       /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
           
            list($page, $limit, $where) = $this->buildTableParames();
            
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
                foreach ($list as $k => $v){
          
                    $user = Db::name("user")->where("id",$v["uid"])->find();
                    if(!empty($user)){
                        $v['nickname'] = $user['nickname'];
                        $v['phone'] = $user['phone'];
                        $v["building_number"] = Db::name("user_building_number")->where("id",$user['building_number_id'])->value("title");
                        $user_unitarea = Db::name("user_unitarea")->where("id",$user['unitarea_id'])->find();
                        if(!empty($user_unitarea)){
                            $v["unitarea"] = $user_unitarea["unit_type"].'/'.$user_unitarea["area"].'m²';
                        }else{
                            $v["unitarea"] = "";
                        }
                    }else{
                        $v['nickname'] = "";
                        $v['phone'] = "";
                        $v['building_number'] = "";
                        $v["unitarea"] = "";
                    } 


                    $service_goods = Db::name("service_goods")->where("id",$v["gid"])->find();
                    if(!empty($service_goods)){
                        $v['service_goodsname'] = $service_goods['title'];
                    }else{
                        $v['service_goodsname'] = "";
                    }
  
                    $list[$k] = $v;
                }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }else{
            // $user = Db::name("user")->order("id desc")->select();
            // $this->assign('user', $user);
            // $service_goods = Db::name("service_goods")->order("id desc")->select();
            // $this->assign('service_goods', $service_goods);
            return $this->fetch();
        }
     
    }

/**
    * @NodeAnotation(title="添加")
    */
    public function add()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['uid'])){
                $this->error('用户不能为空');
            }
            if(empty($post['gid'])){
                $this->error('品牌不能为空');
            }
            $data = [
                'uid'=>$post['uid'],
                'gid'=>$post['gid'],
                'create_time'=>time()
            ];
            try {
                Db::name('my_delegation')->insert($data);
              
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $user = Db::name("user")->order("id desc")->select();
            $this->assign('user', $user);
            $service_goods = Db::name("service_goods")->order("id desc")->select();
            $this->assign('service_goods', $service_goods);
            return $this->fetch();
        }
        
    }

     /**
    * @NodeAnotation(title="编辑")
    */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $post = $this->request->param();
            if(empty($post['uid'])){
                $this->error('用户不能为空');
            }
            if(empty($post['gid'])){
                $this->error('品牌不能为空');
            }
            $data = [
                'uid'=>$post['uid'],
                'gid'=>$post['gid']
            ];
            try {
               $this->model->where('id',$post['id'])->update($data);
              
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $this->success('保存成功');
        }else{
            $post = $this->request->param(); 
            $row = $this->model->where('id',$post['id'])->find();
            $user = Db::name("user")->order("id desc")->select();
            $this->assign('user', $user);
            $service_goods = Db::name("service_goods")->order("id desc")->select();
            $this->assign('service_goods', $service_goods);
           
            $this->assign('row', $row);
        }
        return $this->fetch();
    }





     //取消参团
     public function grant(){
        $post = $this->request->param();
        if(empty($post['id'])){
            $this->error('参数不存在');
        } 
        $this->model->where('id',$post['id'])->update(['is_cancel'=>1]);
        $this->success('操作成功');
    }


    
}